package net.wangds.jpa.service;

import net.wangds.data.WithId;
import net.wangds.jpa.comp.AbstractRemovedComp;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

public abstract class AbstractJpaService<T extends WithId<String>, C extends AbstractRemovedComp<T>> {

    protected abstract C fetchJpaComp();

    @Transactional
    public void remove(String id) {
        fetchJpaComp().remove(id);
    }

    @Transactional
    public void remove(T target) {
        fetchJpaComp().remove(target);
    }

    @Transactional
    public void erase(T target) {
        fetchJpaComp().erase(target);
    }

    @Transactional
    public T save(T target) {
        return fetchJpaComp().save(target);
    }

    public Optional<T> findById(String id) {
        return fetchJpaComp().findById(id);
    }

    public boolean existsById(String id) {
        return fetchJpaComp().existsById(id);
    }

    public Page<T> findAll(Pageable page) {
        return fetchJpaComp().findAll(page);
    }

    public Page<T> findAllWithKeywords(String keywords, Pageable page){
        return fetchJpaComp().findByKeywords(keywords, page);
    }

    public List<T> findAll(Sort sort) {
        return fetchJpaComp().findAll(sort);
    }
}
